var app = getApp() 
// pages/order_list/order_list.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    selected:false,
    selected1:false,
    selected2: false,
    selected3: false,
    selected4: false,
    status:'',
    order_list:[],
    firstRow:0,
    searchKey:'',
    k:'',
    u:'',
    request_num:'',//加载底部使用
    show:0,
    is_show_time: 1,
    search_show: false,
    disabled:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var key = wx.getStorageSync('key')
    var uid = wx.getStorageSync('u')
    var status = options.state || ''
    var firstRow = that.data.firstRow
    that.setData({
      k:key,
      u:uid,
      status:status
    })
    switch (status){
      case 'all':
        that.setData({
          selected: true
        })
      break;
      case 'wait_pay':
        that.setData({
          selected1: true
        })
      break;
      case 'order_payed':
        that.setData({
          selected2: true
        })
      break;
      case 'wait_confirm_goods':
        that.setData({
          selected3: true
        })
      break;
      case 'finish':
        that.setData({
          selected4: true
        })
      break;
      default:
        that.setData({
          selected: true
        })
      break;
    }
    if (key){
      wx.request({
        url: app.configData.shop.api_url + '/index.php?ctl=Buyer_Order&met=physical&typ=json&firstRow=' + firstRow,
        method: 'GET',
        data: { k: key, u: uid, status: status},
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
        console.log(res)
          that.setData({
            order_list:res.data.data.items,
            is_show_time: 0
          })
        }
      })
    }else{
      that.setData({
        is_show_time: 0
      })
    }
  },

  onShow: function () {
    var that = this;
    var key = wx.getStorageSync('key')
    var uid = wx.getStorageSync('u')
    var status = that.data.status
    var firstRow = that.data.firstRow
    that.setData({
      k: key,
      u: uid,
      status: status
    })
    switch (status) {
      case 'all':
        that.setData({
          selected: true
        })
        break;
      case 'wait_pay':
        that.setData({
          selected1: true
        })
        break;
      case 'order_payed':
        that.setData({
          selected2: true
        })
        break;
      case 'wait_confirm_goods':
        that.setData({
          selected3: true
        })
        break;
      case 'finish':
        that.setData({
          selected4: true
        })
        break;
      default:
        that.setData({
          selected: true
        })
        break;
    }
    if (key) {
      wx.request({
        url: app.configData.shop.api_url + '/index.php?ctl=Buyer_Order&met=physical&typ=json&firstRow=' + firstRow,
        method: 'GET',
        data: { k: key, u: uid, status: status },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          console.log(res)
          that.setData({
            order_list: res.data.data.items,
            is_show_time: 0
          })
        }
      })
    } else {
      that.setData({
        is_show_time: 0
      })
    }
  },
//订单列表切换
  order:function(e){
    var that = this;
    var status = e.currentTarget.dataset.status
    var firstRow = 0
    switch (status) {
      case 'all':
        that.setData({
          selected: true,
          selected1:false,
          selected2: false,
          selected3: false,
          selected4: false,
        })
        break;
      case 'wait_pay':
        that.setData({
          selected: false,
          selected1: true,
          selected2: false,
          selected3: false,
          selected4: false,
         
        })
        break;
      case 'order_payed':
        that.setData({
          selected: false,
          selected1: false,
          selected2: true,
          selected3: false,
          selected4: false,
        })
        break;
      case 'wait_confirm_goods':
        that.setData({
          selected: false,
          selected1: false,
          selected2: false,
          selected3: true,
          selected4: false,
        })
        break;
      case 'finish':
        that.setData({
          selected: false,
          selected1: false,
          selected2: false,
          selected3: false,
          selected4: true,
        })
        break;
    }
    if (that.data.k){
      wx.request({
        url: app.configData.shop.api_url + '/index.php?ctl=Buyer_Order&met=physical&typ=json&firstRow=' + firstRow,
        method: 'GET',
        data: { k: that.data.k, u: that.data.u, status: status,  orderkey: that.data.searchKey },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          
          that.setData({
            order_list: res.data.data.items,
            status: status,
            firstRow: firstRow,
            request_num: '',
            is_show_time: 0
          })

        }
      })
      }
  },
  //获取用户输入的用户名
  contentInput: function (e) {
    if (e.detail.value) {
      this.setData({
        content: e.detail.value,
        search_show: true
      })
    } else {
      this.setData({
        search_show: false
      })
    }
  },
  //事件处理函数
  bindViewTap: function () {
    var that = this;
    var content = that.data.content
    if (content == undefined){
      content = ''
    }
    var status = that.data.status
   
    var firstRow = 0
    if (that.data.k){
      wx.request({
        url: app.configData.shop.api_url + '/index.php?ctl=Buyer_Order&met=physical&typ=json&firstRow=' + firstRow,
        method: 'GET',
        data: { k: that.data.k, u: that.data.u, status: status, orderkey: content},
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          that.setData({
            order_list: res.data.data.items,
            firstRow: firstRow,
            request_num:'',
            is_show_time: 0
          })

        }
      })
    }

  },
  //删除订单
  delete_order: function (e) {
    var order_id = e.target.dataset.order_id
    this.setData({
      del_nocancel: true,
      order_id: order_id
    })
  },
  del_confirm: function () {
    var can_id = this.data.order_id
    this.delete_orderss(can_id)
  },
  del_cancel: function () {
    this.setData({
      del_nocancel: false,
    })
  },
  //删除订单
  delete_orderss: function (can_id){
    var that = this
    var order_id = can_id
          wx.request({
            url: app.configData.shop.api_url + '/index.php?ctl=Buyer_Order&met=hideOrder&typ=json',
            method: 'GET',
            data: { k: that.data.k, u: that.data.u, order_id: order_id, user: 'buyer' },
            header: {
              'content-type': 'application/json'
            },
            success: function (res) {
             
              if(res.data.status == 200){
                app.show('成功', 'success')
                that.bindViewTap()
              }else{
                app.show('删除失败', 'loading')
              }
              that.setData({
                del_nocancel: false,
              })
            },fail:function(res){
              app.show('数据加载失败','loading')
            }
          })
   
  },
  //确认订单
  sure_order: function (e) {
    var order_id = e.target.dataset.order_id
    this.setData({
      con_nocancel: true,
      order_id: order_id
    })
  },
  con_confirm: function () {
    var con_id = this.data.order_id
    this.sure_orderss(con_id)
  },
  con_cancel: function () {
    this.setData({
      con_nocancel: false,
    })
  },
  
  //确认订单
  sure_orderss: function (con_id){
    var that = this
    var order_id = con_id
          wx.request({
            url: app.configData.shop.api_url + '/index.php?ctl=Buyer_Order&met=confirmOrder&typ=json',
            method: 'GET',
            data: { k: that.data.k, u: that.data.u, order_id: order_id, user: 'buyer' },
            header: {
              'content-type': 'application/json'
            },
            success: function (res) {
              
              if (res.data.status == 200) {
                app.show('收货完成', 'success')
                that.bindViewTap()
              } else {
                app.show('收货失败', 'loading')
              }
              that.setData({
                con_nocancel: false,
              })
            }, fail: function (res) {
              app.show('数据加载失败', 'loading')
            }
          })
    
  },
  //取消订单
  cancel_order: function (e) {
    var order_id = e.target.dataset.order_id
    this.setData({
      nocancel: true,
      order_id: order_id
    })
  },
  confirm: function () {
    var del_id = this.data.order_id
    this.cancel_orderss(del_id)
  },
  cancel: function () {
    this.setData({
      nocancel: false,
    })
  },
  //取消订单
  cancel_orderss: function (del_id){
    var that = this
    var order_id = del_id
          wx.request({
            url: app.configData.shop.api_url + '/index.php?ctl=Buyer_Order&met=orderCancel&typ=json',
            method: 'GET',
            data: { k: that.data.k, u: that.data.u, order_id: order_id, user: 'buyer' },
            header: {
              'content-type': 'application/json'
            },
            success: function (res) {
            
              if (res.data.status == 200) {
                app.show('取消成功', 'success')
                that.bindViewTap()
              } else {
                app.show('取消失败', 'loading')
              }
              that.setData({
                nocancel: false,
              })
            }, fail: function (res) {
              app.show('数据加载失败', 'loading')
            }
          })
  
  },

  //订单支付
  payOrder:function(e){console.log(e,'支付')
    var that = this
    var uorder_id = e.currentTarget.dataset.payment_number
    var openid = wx.getStorageSync('openid');
    this.setData({ disabled: true });
    wx.request({
      url: app.configData.paycenter.api_url + '/index.php?ctl=Pay&met=createWXOrder&typ=json',
      method: 'GET',
      data: {
        k: that.data.k,
        u: that.data.u,
        uorder_id: uorder_id,
        openid: openid,
        trade_type: 'WXAPP'
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
       console.log(res,'支付2')
        wx.requestPayment({
          'timeStamp': res.data.timeStamp,
          'nonceStr': res.data.nonceStr,
          'package': res.data.package,
          'signType': 'MD5',
          'paySign': res.data.paySign,
          'success': function (res) {
            app.show('支付成功', 'success');
            wx.request({
              url: app.configData.paycenter.api_url + '/index.php?ctl=Pay&met=order_status&typ=json',
              method: 'GET',
              data: {
                k: that.data.k,
                u: that.data.u,
                order_id: uorder_id,
                buyer_id: that.data.u,
              },
              header: {
                'content-type': 'application/json'
              },
              success: function (res) {
                wx.redirectTo({
                  url: '../order_list/order_list'
                })

              }


            })
          },
          'fail': function (res) {
           
          },
          'complete': function (res) {
          
          }
        });
      }
    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this
    if (that.data.k){
      var firstRow = that.data.firstRow+10
      var status = that.data.status
      var content = that.data.content
      if (content == undefined) {
        content = ''
      }
      if (that.data.request_num == 1){
        return false
      }

      wx.request({
        url: app.configData.shop.api_url + '/index.php?ctl=Buyer_Order&met=physical&typ=json&firstRow=' + firstRow,
        method: 'GET',
        data: { k: that.data.k, u: that.data.u, status: status, content: content},
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          if (res.data.data.items.length>0){
            var order_list = that.data.order_list
            for (var z in res.data.data.items) {
              order_list.push(res.data.data.items[z])
            }
            that.setData({
              order_list: order_list,
              firstRow: firstRow,
              is_show_time: 0
            })
          }else{
            that.setData({
              request_num: 1,
              is_show_time: 0
            })
          }
        }
      })
    }
  },
  bindhref: function (e) {
    app.goTop()
    this.setData({
      show: 0
    })
  },
  //一键到顶
  onPageScroll: function (e) {
    // 获取滚动条当前位置
    var that = this
    wx.getSystemInfo({
      success: function (res) {
        
        if (e.scrollTop + res.windowHeight > res.windowHeight) {
          that.setData({
            show: 1
          })
        } else {
          that.setData({
            show: 0
          })
        }
      },
    })
  },
  //删除搜索框内容
  search_clear: function () {
    var that = this
    that.setData({
      searchKey: '',
      search_show: false
    })
  },
  /* 减数 */
  delCount: function (e) {
    //商品id
    var that = this
   
    var k = wx.getStorageSync('key');
    var u = wx.getStorageSync('u');
    var e_id = e.target.id.substring(3);
    var arr = this.data.order_list;
   
   
    for (var i in arr) {
      var del = arr[i].goods_list;
	  var order_nums = arr[i].order_nums;
	  var order_payment_amount = arr[i].order_payment_amount;
      for (var s in del) {
        var jian = del[s]
        if (jian.order_id == e_id) {
          
          if (jian.order_goods_num <= 1) {
            return;
          } else {
            jian.order_goods_num -= 1
			order_nums -= 1
			order_payment_amount -= jian.order_goods_payment_amount
			
			jian.order_goods_amount = parseFloat((Number(jian.order_goods_num) * jian.order_goods_payment_amount))
            // 加载的使用进行网络访问，把需要的数据设置到data数据对象  
            wx.request({
              url: app.configData.shop.api_url + '/index.php?ctl=Buyer_Order&met=editOrderNum&typ=json',
              method: 'GET',
              data: { k: k, u: u, order_id: jian.order_id, num: jian.order_goods_num, },
              header: {
                'content-type': 'application/json'
              },
              success: function (res) {
              }
            })
			
          }
        }
      }
	  
	  //订单总数量与价格
	  arr[i].order_nums = order_nums;
	  arr[i].order_payment_amount = parseFloat(order_payment_amount).toFixed(2);
	  that.setData({
	    order_list: that.data.order_list,
	  })
    }
  },
  /* 加数 */
  addCount: function (e) {
    //商品id
    var that = this
    var k = wx.getStorageSync('key');
    var u = wx.getStorageSync('u');
    var e_id = e.target.id.substring(3);
    var arr = this.data.order_list;
    for (var i in arr) {
      var add = arr[i].goods_list;
	  var order_nums = arr[i].order_nums;
	  var order_payment_amount = arr[i].order_payment_amount;
      for (var s in add) {
        var jia = add[s]
        if (jia.order_id == e_id) {
          jia.order_goods_num = Number(jia.order_goods_num) + 1
		  order_nums += 1
		order_payment_amount = parseFloat(order_payment_amount) + parseFloat(jia.order_goods_payment_amount)
		jia.order_goods_amount = parseFloat((Number(jia.order_goods_num) * jia.order_goods_payment_amount))
          // if (jia.order_goods_num > jia.goods_base.goods_stock) {
          //   jia.order_goods_num = jia.goods_base.goods_stock
          //   that.show("不可超过商品库存")
          // } else {
            wx.request({
              url: app.configData.shop.api_url + '/index.php?ctl=Buyer_Order&met=editOrderNum&typ=json',
              method: 'GET',
              data: {
                k: k,
                u: u,
                order_id: jia.order_id,
                num: jia.order_goods_num,
              },
              header: {
                'content-type': 'application/json'
              },
              success: function (res) {
              }
            })
          }
        // }
      }
	  //订单总数量与价格
	  arr[i].order_nums = order_nums;
	  arr[i].order_payment_amount = parseFloat(order_payment_amount).toFixed(2);
	  that.setData({
	    order_list: that.data.order_list,
	  })
    }
	
  },
  /*购物车输入数量*/
  goodsNum: function (e) {
    //商品id
    var that = this
    var k = wx.getStorageSync('key');
    var u = wx.getStorageSync('u');
    var e_id = e.target.dataset.id;
    var inputNum = e.detail.value
    var arr = that.data.order_list;
    for (var i in arr) {
      var del = arr[i].goods_list;
	  var order_nums = arr[i].order_nums;
	  var order_payment_amount = arr[i].order_payment_amount;
      for (var s in del) {
        var jian = del[s]
        if (jian.order_id == e_id) {
			if (Number(inputNum) == 0) {
            // if (jian.flag == true) {
            //   that.data.total -= Number(jian.goods_num * jian.now_price);
            //   jian.goods_num = 1;
            //   that.data.total += Number(jian.goods_num * jian.now_price);
            // } else {
              
            // }
			order_nums = Number(order_nums) - Number(jian.order_goods_num) + 1
			jian.order_goods_num = 1;
			order_payment_amount = parseFloat(order_payment_amount) - parseFloat(jian.order_goods_amount) + parseFloat(jian.order_goods_payment_amount)
			jian.order_goods_amount = parseFloat((Number(inputNum) * jian.order_goods_payment_amount))
			arr[i].order_nums = order_nums;
			arr[i].order_payment_amount = parseFloat(order_payment_amount).toFixed(2);
			  that.setData({
				order_list: that.data.order_list
			  })
            return false
          }
		  
		  
		  order_nums = Number(order_nums) - Number(jian.order_goods_num) + Number(inputNum)
		  jian.order_goods_num = Number(inputNum)
		  order_payment_amount = parseFloat(order_payment_amount) - parseFloat(jian.order_goods_amount) + parseFloat((Number(inputNum) * jian.order_goods_payment_amount))
		  jian.order_goods_amount = parseFloat((Number(inputNum) * jian.order_goods_payment_amount))
  //         if (Number(inputNum) > Number(jian.goods_base.goods_stock)) {
  //           if (jian.flag == true) {
  //             that.data.total -= Number(jian.goods_num * jian.now_price);
  //             jian.goods_num = jian.goods_base.goods_stock
  //             that.data.total += Number(jian.goods_num * jian.now_price);
  //           } else {
  //             jian.goods_num = jian.goods_base.goods_stock
  //           }
  
  //           that.show("不可超过商品库存")
  //           that.setData({
  //             glistAll: that.data.glistAll,
  //             total: that.data.total,
  //             count_price: that.data.total.toFixed(2),
  //           })
  //         } else {
            // 加载的使用进行网络访问，把需要的数据设置到data数据对象  
            wx.request({
              url: app.configData.shop.api_url + '/index.php?ctl=Buyer_Order&met=editOrderNum&typ=json',
              method: 'GET',
              data: { k: k, u: u, order_id: jian.order_id, num: inputNum, },
              header: {
                'content-type': 'application/json'
              },
              success: function (res) {
              }
            })
          // }
        }
      }
	  //订单总数量与价格
	  arr[i].order_nums = order_nums;
	  arr[i].order_payment_amount = parseFloat(order_payment_amount).toFixed(2);
	  that.setData({
	    order_list: that.data.order_list
	  })
    }
  },
  //跳转至订单详情页面
  goToVrOrderDetail: function (e) {
    var that = this
    var order_id = e.currentTarget.dataset.order_id
    wx.navigateTo({
      url: '../vr_order_detail/vr_order_detail?order_id=' + order_id
    })
  },
  goToOrderDetail: function (e) {
    var that = this
    var order_id = e.currentTarget.dataset.order_id
    wx.navigateTo({
      url: '../order_detail/order_detail?order_id=' + order_id
    })
  },
  
})